﻿
using DocumentFormat.OpenXml.Wordprocessing;
using LYR_RPA.models.project;
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;


namespace LYR_RPA.dialog
{

    public partial class TimeDialog : UserControl
    {
        public static DateTime date;
        private Execute execute;
        public TimeDialog(Execute execute)
        {
            InitializeComponent();
            this.execute = execute;
            date = execute.ExecuteTime;
            year.ItemsSource = Enumerable.Range(date.Year, 2099 - date.Year).ToList();
            year.SelectedIndex=0;
            month.ItemsSource=Enumerable.Range(1,12).ToList();
            month.SelectedIndex=date.Month-1;
            days.ItemsSource=Enumerable.Range(1, DateTime.DaysInMonth(Int32.Parse(year.Text.ToString()), Int32.Parse(month.Text.ToString()))).ToList();
            days.SelectedIndex=date.Day-1;
            hour.ItemsSource= Enumerable.Range(0, 23).ToList();
            hour.SelectedIndex=date.Hour;
            minutes.ItemsSource = Enumerable.Range(00, 60).ToList();
            minutes.SelectedIndex=date.Minute;
            second.ItemsSource = Enumerable.Range(00, 60).ToList();
            second.SelectedIndex=date.Second;
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {

            Window.GetWindow(this).Close();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            
            try
            {
                string dateStr = year.Text + "/" + month.Text + "/" + days.Text + " " + hour.Text + ":" + minutes.Text + ":" + second.Text;
                date =DateTime.Parse(dateStr);
                if (date>=DateTime.Now)
                {
                    this.execute.ExecuteTime = date;
                    this.execute.Msg = "执行时间："+dateStr;
                    Window.GetWindow(this).Close();
                }
                else
                {
                    tip.Text = "选择日期不能小于当前日期";
                }
            }
            catch (Exception ex)
            {
                tip.Text = "日期格式出现问题！";
                Console.WriteLine(ex.Message);
            }
            
            
            
        }

        private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                Window.GetWindow(this).DragMove();
            }
        }
    }
}
